using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 项目计划
    /// 版 本：V2.6.200509
    /// 版 权：引迈信息技术有限公司（https://www.yinmaisoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-11-16 
    /// </summary>
    public class ProjectGanttBll
    {
        private ProjectGanttIService service = new ProjectGanttService();

        /// <summary>
        /// 项目列表(分页)
        /// </summary>
        /// <returns></returns>
        public async Task<PageResult<ProjectGanttEntity>> GetList(PageInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 项目列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<ProjectGanttEntity>> GetList()
        {
            try
            {
                return await service.GetList();
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 任务列表
        /// </summary>
        /// <param name="projectId">项目Id</param>
        /// <returns></returns>
        public async Task<List<ProjectGanttEntity>> GetTaskList(string projectId)
        {
            try
            {
                var entity = await this.GetInfo(projectId);
                var list = await service.GetTaskList(projectId);
                list.Add(entity);
                return list;
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<ProjectGanttEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 判断是否允许删除
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<bool> AllowDelete(string id)
        {
            var subdataCount = (await service.GetList()).FindAll(m => m.ParentId == id).Count;
            return (subdataCount == 0);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(ProjectGanttEntity entity)
        {
            try
            {
                return await service.Delete(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(ProjectGanttEntity entity)
        {
            try
            {
                entity.Id = CommonHelper.Guid;
                if (entity.EnabledMark == null)
                    entity.EnabledMark = 1;
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                return await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(string id, ProjectGanttEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 重复验证（名称）
        /// </summary>
        /// <param name="fullName">名称</param>
        /// <param name="id">键值</param>
        /// <returns></returns>
        public async Task<bool> IsExistByFullName(string fullName, string id)
        {
            try
            {
                var whereLambda = ExtLinq.True<ProvinceEntity>();
                whereLambda = whereLambda.And(m => m.FullName == fullName);
                if (!string.IsNullOrEmpty(id))
                    whereLambda = whereLambda.And(m => m.Id != id);
                return await service.IsExist(whereLambda);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }

        }
        /// <summary>
        /// 重复验证（编码）
        /// </summary>
        /// <param name="enCode">编码</param>
        /// <param name="id">键值</param>
        /// <returns></returns>
        public async Task<bool> IsExistByEnCode(string enCode, string id)
        {
            try
            {
                var whereLambda = ExtLinq.True<ProvinceEntity>();
                whereLambda = whereLambda.And(m => m.EnCode == enCode);
                if (!string.IsNullOrEmpty(id))
                    whereLambda = whereLambda.And(m => m.Id != id);
                return await service.IsExist(whereLambda);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
    }
}
